package
{
	import flash.events.*;
	import flash.net.*;
	import flash.text.*;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import widok.inzynierka;
	
	
	
	
	public class login
	{
		
		public var _instance : inzynierka
		[Bindable]
		public var grupawyk:String;
		[Bindable]
		public var grupacw:String;
		[Bindable]
		public var grupalab:String;
		[Bindable]
		public var name:String;
		[Bindable]
		public var pierwszy:String;
		[Bindable]
		public var email:String;
		
		
			
			public function validate(userlogin:String,userpass:String):void
			{
				var pattern:RegExp=/[0-9]+/;
				trace( pattern.test(userlogin));
				trace( pattern.test(userpass));
				if(userlogin.length==11 && userpass.length>=3 && pattern.test(userlogin)==true)
				{
					
					
					
				
				var phpVars:URLVariables = new URLVariables();
				var phpFileRequest:URLRequest = new URLRequest("php/login.php");
				phpFileRequest.method = URLRequestMethod.POST;
				phpFileRequest.data = phpVars;
				var phpLoader:URLLoader = new URLLoader();
				phpLoader.dataFormat = URLLoaderDataFormat.TEXT;
				phpLoader.addEventListener(Event.COMPLETE, showResult);
				
				
				phpVars.username = userlogin
				phpVars.password = userpass
				
				
				try {
					phpLoader.load(phpFileRequest);
				}
				catch(errObject:Error) {
					trace(errObject.message);
				}
				
				} else Alert.show("Wypełnij poprawnie");
			}
			
			public function showResult (event:Event):void {
				
			
				
				if (event.target.data != 'fail')
				{
					var dane:String=event.target.data;
					var arr:Array =dane.split(" ");
					
					grupawyk=arr[1];
					grupacw=arr[2];
					grupalab=arr[3];
					pierwszy=arr[4];
					email=arr[5];
					
					_instance.clock();
					
					name="Witaj "+arr[0];
					if(pierwszy!="1")
					{
					_instance.currentState="zalogowany";
					}
					else
					{
				    _instance.currentState="pierwszyraz";
					}
					_instance.dataGrid_creationCompleteHandler();
					
					
					
				}
				else 
				{
					
					Alert.show("bledny login lub haslo")
				}
				
					
					
				
			}
			
			
		}
	}
